package com.example.smallmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smallmall.common.api.CommonResult;
import com.example.smallmall.entity.SpMenus;
import com.example.smallmall.service.ISpMenusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WSGL
 * @since 2020-07-23
 */
@Api(tags = "左侧菜单权限")
@RestController
@RequestMapping("/menus")
public class SpMenusController {
    @Resource
    private ISpMenusService iSpMenusService;

    @ApiOperation("菜单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Map<String, Object>>> listMenu() {
        List<Map<String, Object>> maps = iSpMenusService.selectAllMenu();
        return CommonResult.success(maps);
    }

    @ApiOperation("根据id查询菜单")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> listMenusById(@PathVariable("id") Integer id) {
        Map<String, Object> map = iSpMenusService.getMap(new QueryWrapper<SpMenus>().lambda().eq(SpMenus::getMenusId, id));
        Object obj = 0;
        if (map.get("parentId") == obj) {
            map.remove("parentId");
        }
         // 判断 parentId是否为0
//        if (map.containsValue(0)) {
//            map.remove("parentId");
//        }
        if (!map.isEmpty()) {
            return CommonResult.success(map);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("添加菜单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Boolean> createMenus(@RequestBody SpMenus spMenus) {
        if (spMenus.getParentId() == null) {
            spMenus.setParentId(0);
        }
        boolean save = iSpMenusService.save(spMenus);
        if (save) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("修改菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<Boolean> updateMenus(@PathVariable("id") Integer id, @RequestBody SpMenus spMenus) {
        boolean update = iSpMenusService.update(spMenus, new QueryWrapper<SpMenus>().eq("menus_id", id));
        if (update) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("删除菜单")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Boolean> deleteMenus(@PathVariable("id") Integer id) {
        boolean remove = iSpMenusService.removeById(id);
        if (remove) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("操作失败");
        }
    }
}
